// import "babel-polyfill";
// import Promise from "es6-promise";
// Promise.polyfill();
import Vue from 'vue'
import VueRouter from 'vue-router'
import Vuex from 'vuex'
import VueI18n from 'vue-i18n'
import './processConfig.js'

import App from './App.vue'
import './tools/index.js'
import filters from './filters.js'
import store from './store/index.js'
import ripples, { configSystemRoute } from 'ripples'
import Api_SSDP_Mapping from './SSDPConfig'
import routeConfig from './router'
import iView from 'view-design'
import 'ripples/dist/styles/ripples.css'
// import '@/styles/font/iconfont.css'
// import '@/styles/common.less'
// import '@/styles/reset.less'
import '@/styles/index.less'
// 集团风格主题
// import '@ripples/iview-crc-theme'
// 华润云风格主题
import '@ripples/iview-crcloud-theme'

// import ripplesEnSource from 'ripples/dist/locale/en-US'
// import ripplesZhSource from 'ripples/dist/locale/zh-CN'
// import langSource from './lang'

// import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import format from './components/common/Format'
import { outLineHandle,formatNumberWithUnit,toFixPercent,toPercentWithoutUnit,removeMinusSign,formatAbnormalData } from "@/tools/util";
import { CoreModuleName } from "./const.js";
import Overview from "./views/overview/index";
import scroll from 'vue-seamless-scroll'
Vue.use(scroll)
Vue.component('Format', format)
Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(VueI18n)

Vue.config.devtools = window.RPConfig.devtools

for (let filter of filters) {
  Vue.filter(filter.name, filter.fn)
}

// 自动设置语言
// const navLang = navigator.language
// const localLang = navLang === 'zh-CN' || navLang === 'en-US' ? navLang : false
// const lang = window.localStorage.getItem('language') || localLang || 'zh-CN'
// Vue.config.lang = lang
const detectZoom = () => {
  let ratio = 0,
    screen = window.screen,
    ua = navigator.userAgent.toLowerCase();
  if (window.devicePixelRatio !== undefined) {
    ratio = window.devicePixelRatio;
  } else if (~ua.indexOf('msie')) {
    if (screen.deviceXDPI && screen.logicalXDPI) {
      ratio = screen.deviceXDPI / screen.logicalXDPI;
    }
  } else if (
    window.outerWidth !== undefined &&
    window.innerWidth !== undefined
  ) {
    ratio = window.outerWidth / window.innerWidth;
  }
  if (ratio) {
    ratio = Math.round(ratio * 100);
  }
  return ratio;
};
 
const m = detectZoom();
const userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf("mac") >= 0) {
  // return "Mac";
  document.body.style.zoom = 1
} else {
  document.body.style.zoom = 100 / Number(m);
}

var routes = []
configSystemRoute(routes, routeConfig.procure)
routes.push(routeConfig.overview)
const router = new VueRouter({ routes })

Vue.use(ripples, {
  router,
  store,
  Api_SSDP_Mapping,
  app: {
    title: '通用开发平台',
    //配置主页路由信息
    home: {
      path: '/overview',
      title: '门户',
      name: 'home_index'
    }
  }
})
// 在面包屑导航的tagList中添加门户页路由，防止点击菜单查找不到
// store.commit(CoreModuleName + "/setPublicPages", "/overview");
let overview = {
  path: '/overview',
  name: 'home_index',
  component: Overview
};
store.commit(CoreModuleName + "/setTagsList", [overview]);
router.beforeEach((to, from, next) => {
  outLineHandle(router, to)
  iView.LoadingBar.start()
  $channel.$emit('RouterChangeBefore')
  $channel.$emit('ActionBarEvent', [])
  next()
  iView.LoadingBar.finish()
})

router.afterEach((to, from, failure) => {
  if (
    !store.state.user
    || !store.state.user.currentSysInfo
    || !store.state.user.currentSysInfo.id) {
    store.dispatch('sasspWeb/getValueList')
    store.dispatch('user/getUserInfo')
  }
  store.dispatch('sasspWeb/getUnhandledNum')
  iView.LoadingBar.finish()
  window.scrollTo(0, 0)
})

// 多语言配置
// Vue.locale('zh-CN', Object.assign(ripplesZhSource, langSource['zh-CN']))
// Vue.locale('en-US', Object.assign(ripplesEnSource, langSource['en-US']))

window.$channel = window.$channel || new Vue()
window.$channel.$on('LeftSideMenuMounted', () => {
  
})

window.$channel.$on('unhandledNumChanged', total => {
  setTimeout(() => {
    // 待办数量大于0且待办菜单渲染完成
    if (total > 0 && document.querySelector('.sidebar_menu-unhandled a')) {
      let element = document.getElementById("unhandledNumSup")
      if (element) {
        element.parentNode.removeChild(element)
      }
      let sup = document.createElement('sup')
      let content = document.createTextNode(total);
      let baseRight = 82
      let right = (baseRight - (''+total).length + 'px')
      sup.className = 'ivu-badge-count'
      sup.style.right = right
      sup.style.top = '16px'
      sup.id = 'unhandledNumSup'
      sup.appendChild(content);
      document.querySelector('.sidebar_menu-unhandled a').appendChild(sup)
    // 待办数量为0且菜单渲染完成
    } else if (total == 0 && document.querySelector('.sidebar_menu-unhandled a')) {
      // 菜单上已经有待办数量提醒
      let element = document.getElementById("unhandledNumSup")
      if (element) {
        element.parentNode.removeChild(element)
      }
    }
  }, 1000);
})

let vueOption = {
  el: '#app',
  router, // 路由配置
  store,
  render: h => h(App),
  data: {
    currentPageName: ''
  },
  mounted() {
    this.currentPageName = this.$route.name
  },
  created() {},
}

window.$app = new Vue(vueOption)
window.$store = store
Vue.prototype.$store = store
Vue.prototype.formatNumberWithUnit = formatNumberWithUnit
Vue.prototype.toFixPercent = toFixPercent
Vue.prototype.toPercentWithoutUnit = toPercentWithoutUnit
Vue.prototype.removeMinusSign = removeMinusSign
Vue.prototype.formatAbnormalData = formatAbnormalData
if (localStorage.Authorization) {
  store.dispatch('sasspWeb/getValueList')
  store.dispatch('user/getUserInfo')
}

